//
//  LoginWindowController.m
//  HiQ
//
//  Created by arvintan on 12-7-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LoginWindowController.h"

@interface LoginWindowController ()

@end

@implementation LoginWindowController

@synthesize _textName;
@synthesize _textPassword;
@synthesize _buttonLogin;
@synthesize _buttonRegister;
@synthesize _buttonHeadImage;


- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];

}

- (void) awakeFromNib
{
    //设置登陆界面图像
    NSImage *headImage = [NSImage imageNamed:@"MacQQ logo.icns"];
    [_buttonHeadImage setImage:headImage];
}


-(void)loginError:(NSString *)infoText
{
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:NSLocalizedString(@"确定", nil)];
    [alert setMessageText:NSLocalizedString(@"登陆失败",nil)];
    [alert setInformativeText:NSLocalizedString(infoText, nil)];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert beginSheetModalForWindow:[NSApp mainWindow] modalDelegate:self didEndSelector:@selector(sheetClosed:returnCode:contextInfo:) contextInfo:nil];
}

-(void)sheetClosed:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
    if (returnCode == NSAlertDefaultReturn) {
        [(__bridge_transfer NSWindow *)contextInfo close];
    }
}

#pragma mark -
#pragma mark button action

-(IBAction)onLogin:(id)sender
{
	NSString *name = [_textName stringValue];
	if ([name length] == 0)
	{
		[self loginError:NSLocalizedString(@"请输入用户账号", nil)];
		return;
	}
    
    NSString *pwd = [_textPassword stringValue];
	if ([pwd length] < 3 || [pwd length] > 15)
	{
		[self loginError:NSLocalizedString(@"密码长度必须在3-15位之间", nil)];
		return;
	}

    NSString *serverIp = [[NSUserDefaults standardUserDefaults] stringForKey:@"SERVER_IP"];
    NSUInteger serverPort = [[[NSUserDefaults standardUserDefaults] stringForKey:@"SERVER_PORT"] intValue];
      
	if ([serverIp length] == 0)
	{
		[self loginError:NSLocalizedString(@"请输入服务器地址", nil)];
		return;
	}
    
	if (serverPort <= 0 || serverPort > 65535)
	{
		[self loginError:NSLocalizedString(@"请输入端口号", nil)];
		return;
	}
    
    //设置逻辑类的代理
    _login = [[LoginServer alloc]initWithHost:serverIp andPort:serverPort];
    _login.delegate = self;
    [_login sendLoginMsg:name Password:pwd];
    

    

    //test 原sendMsg接口
//    NSString *loginMsg = [NSString stringWithFormat:@"%@/%@",_name,pwd];
//    [_login sendMsg:loginMsg];
//    test
//    [self loginComplete:YES]; 
}

-(IBAction)onRegister:(id)sender
{
    //打开注册界面
    _registerWindowController = [[RegisterWindowController alloc] initWithWindowNibName:@"RegisterWindowController"];
    [_registerWindowController showWindow:self];
}

-(IBAction)onServerSetting:(id)sender
{
    _serverSettingWindowController = [[ServerSettingWindowController alloc] initWithWindowNibName:@"ServerSettingWindowController"];
    [_serverSettingWindowController serverSetting:self];
}


#pragma mark -
#pragma mark loginServer delegate

- (void) loginTimeOut
{
    [self loginError:NSLocalizedString(@"登陆超时", nil)];
}



- (void) loginComplete:(bool)flag UserInfo:(Buddy *)userInfo
{
    if (flag) {
        _mainWindowController = [[MainWindowController alloc] initWithWindowNibName:@"MainWindowController"];
        [_mainWindowController showWindow:self];
        
        [_mainWindowController initWithBuddyInfo:userInfo];
        [self close];
    }
    else {
        [self loginError:NSLocalizedString(@"账号或者密码错误", nil)];
    }
}

- (void) loginRepeat:(bool)flag
{
    if(flag)
    {
        [self loginError:NSLocalizedString(@"重复登陆", nil)];
    }
}

@end
